package com.atguigu.educenter.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.commonutils.ordervo.UcenterMemberOrder;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.entity.vo.RegisterVo;
import com.atguigu.educenter.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-08-12
 */
@RestController
@RequestMapping("/educenter/member")
//@CrossOrigin
public class UcenterMemberController {

    @Autowired
    UcenterMemberService memberService;

    @PostMapping("/login")
    public R loginUser(@RequestBody UcenterMember ucenterMember){
        String token=memberService.login(ucenterMember);
        return R.ok().data("token",token);
    }

    //注册
    @PostMapping("register")
    public R registerUser(@RequestBody RegisterVo registerVo){
        memberService.register(registerVo);
        return R.ok();
    }

    //根据token获取用户信息，用于前端显示
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){
        //使用jwt获取用户id
        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        System.out.println("扫码登录的id"+memberIdByJwtToken);
        UcenterMember byId = memberService.getById(memberIdByJwtToken);
        return R.ok().data("userInfo",byId);
    }

    //远程调用所用的方法，根据id获取对应的用户信息
    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable String id){
        UcenterMember ucenterMember = memberService.getById(id);
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(ucenterMember,ucenterMemberOrder);
        return ucenterMemberOrder;
    }

    //获取每天日期的注册人数
    @GetMapping("/countRegister/{day}")
    public R countRegister(@PathVariable String day){
        int count=memberService.countRegister(day);
        return R.ok().data("count",count);
    }

}

